package com.zb.sannuo.testing.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zb.sannuo.common.domain.*;
import com.zb.sannuo.common.domain.search.PatientDrugstoreSearch;
import com.zb.sannuo.common.result.Result;
import com.zb.sannuo.common.result.ResultCode;
import com.zb.sannuo.testing.service.TestingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/testing")
@Api(tags = "检测者管理模块", description = "对检测者进行CRUD操作")
@CrossOrigin
public class TestingController {

    @Resource
    private TestingService testingServiceImpl;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ApiOperation(value = "检测者分页查询",tags = "分页查询检测者信息")
    public Result queryIdPatient(Integer page,Integer limit,String searchParams)throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        PatientDrugstoreSearch search =null;
        if (searchParams != null && !"".equals(searchParams)){
            search = objectMapper.readValue(searchParams, PatientDrugstoreSearch.class);
        }
        Page page1 = testingServiceImpl.selectListPage(page, limit, search);
        System.out.println(page1.getTotal());
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),page1.getRecords());
    }


    @RequestMapping(value = "{patientId}",method = RequestMethod.GET)
    @ApiOperation(value = "根据id修改状态",notes = "根据id修改检测者状态")
    public Result updatePatient(@PathVariable("patientId") Integer patientId) throws Exception{
        int i = testingServiceImpl.updateStatusId(patientId);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.ok(ResultCode.FAIL,null);
        }
    }

    @RequestMapping(value = "updatePatient",method = RequestMethod.POST)
    @ApiOperation(value = "根据id修改",notes = "根据id修改检测者信息",httpMethod = "POST")
    public Result updatePatientId(SccPatient SccPatient) throws Exception{
        UpdateWrapper<SccPatient> wrapper = new UpdateWrapper<>();
        wrapper.eq("patient_id", SccPatient.getPatientId());
        wrapper.set("phone", SccPatient.getPhone());
        wrapper.set("sex", SccPatient.getSex());
        wrapper.set("name", SccPatient.getName());
        wrapper.set("age", SccPatient.getAge());
        wrapper.set("id_card", SccPatient.getIdCard());
        wrapper.set("update_time",new Date());
        boolean update = testingServiceImpl.update(wrapper);
        if(update){
            return Result.ok(ResultCode.SUCCESS,null);
        }else{
            return Result.ok(ResultCode.FAIL,null);
        }

    }

    @RequestMapping(value = "addAll",method = RequestMethod.POST)
    @ApiOperation(value = "增加",notes = "增加")
    public Result addPatient(Patient patient, SccUserLogin sccUserLogin, Role role, Drugstore drugstore) throws Exception{
        int i = testingServiceImpl.addAll(patient,sccUserLogin,role,drugstore);
        if(i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            return Result.ok(ResultCode.FAIL,null);
        }
    }

}
